
/*
 * jjyun_delete.c
 * 删除文件功能
 * Created on: 2018年5月2日
 * Author: cfm
 */

/**
 * 删除文件执行功能
*/
typedef struct jjyun_delete_ctx_s
{
    ngx_http_request_t *r;
    ngx_str_t fileid;
} jjyun_delete_ctx_t;

static ngx_int_t jjyun_delete_handler(ngx_http_jjyun_ctx_t *jjctx);

void jjyun_delete_file_callback(ngx_fdfs_upstream_t *u)
{
    ngx_http_request_t *r = u->r;
    ngx_http_jjyun_ctx_t *jjctx = (ngx_http_jjyun_ctx_t *)u->data;
    ngx_buf_t **b = &jjctx->response;

    jjyun_format_response(r, b, u->nerr, "");
    ngx_http_finalize_request(r, jjyun_send_response(jjctx));
}

static ngx_int_t jjyun_delete_handler(ngx_http_jjyun_ctx_t *jjctx)
{
    ngx_int_t rc;
    ngx_http_request_t *r = jjctx->r;
    jjyun_delete_ctx_t *ctx;
    ngx_buf_t **b = &jjctx->response;
    if (jjctx->type_ctx == NULL)
    {
        jjctx->type_ctx = ngx_pcalloc(r->pool, sizeof(jjyun_delete_ctx_t));
        if (jjctx->type_ctx == NULL)
        {
            return NGX_HTTP_INTERNAL_SERVER_ERROR;
        }
    }
    ctx = jjctx->type_ctx;
    ctx->r = r;
    if (ngx_http_arg(r, (u_char *)"fileid", ngx_strlen("fileid"), &ctx->fileid) != NGX_OK)
    {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    if ((rc = ngx_jjyun_delete_file(r, &ctx->fileid, jjctx, jjyun_delete_file_callback)) != NGX_OK)
    {
        jjyun_format_response(r, b, rc, "");
        jjyun_send_response(jjctx);
        return NGX_ERROR;
    }
    r->main->count++;

    return NGX_DONE;
}